﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core.DAL.SubSonicDAL;

namespace Medianamik.Core.Logging
{
    public class SubSonicLogEntry : ILogEntry
    {
        private ActivityLog _log;

        internal SubSonicLogEntry(ActivityLog log)
        {
            if (log == null)
                throw new ArgumentNullException("log");
            _log = log;
        }

        internal SubSonicLogEntry(Guid id) : this(ActivityLog.FetchByID(id))
        {
        }

        public Guid LogID 
        {
            get
            {
                return _log.ActivityLogId;
            }
        }
        public Guid? ObjectID 
        {
            get
            {
                return _log.ObjectId;
            }
        }
        public string ObjectName 
        {
            get
            {
                return _log.ObjectName;
            }
        }
        public string Message 
        {
            get
            {
                return _log.Message;
            }
        }
        public DateTime LoggedOn 
        {
            get
            {
                return _log.CreatedOn;
            }
        }
        public string LoggedBy 
        {
            get
            {
                return _log.CreatedBy;
            }
        }
        public LogEntryType Type
        {
            get
            {
                return (LogEntryType)_log.Type;
            }
        }
        public string IP 
        {
            get
            {
                return _log.Ip;
            }
        }
        public string UserAgent
        {
            get
            {
                return _log.UserAgent;
            }
        }
        public string ShortUserAgent
        {
            get
            {
                if (!string.IsNullOrEmpty(UserAgent))
                {
                    int pos = UserAgent.IndexOf(' ');
                    if (pos != -1)
                        return UserAgent.Substring(0, pos);
                    else
                        return UserAgent;
                }
                else
                    return string.Empty;
            }
        }
    }
}
